package newsDemo.controller;

import newsDemo.pojo.Result;
import newsDemo.pojo.agree;
import newsDemo.pojo.news;
import newsDemo.service.Impl.newsServiceImpl;
import newsDemo.service.Impl.userServiceImpl;
import newsDemo.service.agreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/agree")
public class agreeController {

    @Autowired
    private agreeService agreeService;
    @Autowired
    private newsServiceImpl newsService;
    @Autowired
    private userServiceImpl userService;

    @PostMapping("/check")
    public Result check(@RequestBody agree agree) {
        return Result.success(agreeService.check(agree));
    }

    @PostMapping("/agreeOrDisAgree")
    public Result agreeOrDisAgree(@RequestBody Map<String, Object> map) {
        agree agree = new agree((int) map.get("USER_ID"), (int) map.get("NEWS_ID"));
        if(agree.getNewsId() > 25 || agree.getNewsId() < 0)  return Result.success();
        boolean flag = (boolean) map.get("status");
        int temp = agreeService.change(agree, flag);
        if(temp == 0)  return Result.success();
        else if(temp == 1) return Result.fail(10004, "不能重复点赞");
        else return Result.fail(10003, "不能重复取消点赞");
    }

    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> map) {
        int id = (int) map.get("USER_ID");
        List<agree> list = agreeService.getAgreeList(id);
        List<news> news = new ArrayList<>();
        for(agree agree : list) {
            news news1 = newsService.findById(agree.getNewsId());
            news.add(news1);
        }
        if(news.size() > 0) return Result.success(news);
        return Result.fail(10003, "用户点赞列表为空");
    }
}
